import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-11-10
 * Time: 18:39
 */
public class RedisDemoString {


    public static void test1(Jedis jedis) {
        System.out.println("mget 和 mset");
        jedis.flushAll();

        jedis.mset("key1", "111", "key2", "222", "key3", "333");

        // 顺序和传入的 key 保持一致
        List<String> values = jedis.mget("key1", "key2", "key888","key3");
        System.out.println("values:" + values);
    }

    public static void test2(Jedis jedis) {
        System.out.println("getrange 和 setrange");
        jedis.flushAll();

        jedis.set("key", "0123456789");
        String result = jedis.getrange("key", 2, 5);
        System.out.println("result:" + result);

        long change = jedis.setrange("key", 2, "22222");
        System.out.println("change:" + change);
        System.out.println("value:" + jedis.get("key"));
    }

    public static void test3(Jedis jedis) {
        System.out.println("append");
        jedis.flushAll();

        jedis.set("key", "abcdef");
        jedis.append("key", "123");

        System.out.println("value:" + jedis.get("key"));
    }

    public static void test4(Jedis jedis) {
        System.out.println("incr 和 decr");
        jedis.flushAll();


        jedis.set("key", "100");

        long result = jedis.incr("key");
        System.out.println("result:" + result);

        System.out.println("value:" + jedis.get("key"));

        result = jedis.decr("key");
        System.out.println("result:" + result);
        System.out.println("value:" + jedis.get("key"));
    }


    public static void main(String[] args) {
        try (JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888")) {
            try (Jedis jedis = jedisPool.getResource()) {
                test4(jedis);
            }

        }

    }
}
